package com.zhuchaooasystem.ui.home.service;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.ServiceViewHolder;
import com.zhuchaooasystem.base.BaseActivity;
import com.zhuchaooasystem.common.Key;
import com.zhuchaooasystem.entity.ServiceEntity;
import com.zhuchaooasystem.presenter.home.service.ServiceSearchContract;
import com.zhuchaooasystem.presenter.home.service.ServiceSearchPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class ServiceSearchActivity extends BaseActivity<ServiceSearchPresenter>
        implements ServiceSearchContract.View, SwipeRefreshLayout.OnRefreshListener
        , RecyclerArrayAdapter.OnLoadMoreListener {


    @BindView(R.id.service_search_iv_back)
    ImageView serviceSearchIvBack;
    @BindView(R.id.Service_search_et)
    EditText ServiceSearchEt;
    @BindView(R.id.Service_search_iv_layout)
    RelativeLayout ServiceSearchIvLayout;
    @BindView(R.id.service_search_recyclerView)
    EasyRecyclerView serviceSearchRecyclerView;

    private int page = 1;
    private RecyclerArrayAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_service_search;
    }

    @Override
    protected ServiceSearchPresenter getPresenter() {
        return new ServiceSearchPresenter(mContext, this);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initView() {
//        //设置EditView焦点获取
//        ServiceSearchEt.setFocusable(false);
//
//        ServiceSearchEt.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View view, MotionEvent motionEvent) {
//
//                ServiceSearchEt.setFocusableInTouchMode(true);
//
//                return false;
//            }
//        });
        serviceSearchRecyclerView.setRefreshingColor(Key.RECYCLEVIEW_REFRESH_COLORS);
        serviceSearchRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        serviceSearchRecyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new ServiceViewHolder(parent);
            }
        });
        serviceSearchRecyclerView.setEmptyView(R.layout.view_empty);
        adapter.setNoMore(R.layout.view_nomore, new RecyclerArrayAdapter.OnNoMoreListener() {
            @Override
            public void onNoMoreShow() {
                adapter.resumeMore();
            }

            @Override
            public void onNoMoreClick() {
                adapter.resumeMore();
            }
        });
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });


        serviceSearchRecyclerView.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);

    }

    @Override
    protected void initDataAndLoadData() {

    }

    @OnClick({R.id.service_search_iv_back, R.id.Service_search_iv_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回键
            case R.id.service_search_iv_back:

                ServiceSearchActivity.this.finish();

                break;
            //搜索
            case R.id.Service_search_iv_layout:
                page = 1;
                mPresenter.downSearchData(page, true);
                break;
        }
    }

    @Override
    public String getSearch() {
        return ServiceSearchEt.getText().toString();
    }

    @Override
    public void showData(ServiceEntity entity) {
        adapter.clear();
        adapter.addAll(entity.getResult().getSuccessful().getResponseFlow());
    }

    @Override
    public void showMoreData(ServiceEntity entity) {
        adapter.addAll(entity.getResult().getSuccessful().getResponseFlow());
    }

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.downSearchData(page, false);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.downSearchData(page, false);
    }
}
